import 'package:flutter/material.dart';
import 'package:flutter_demo/ui/pages/read/tools/book_fx.dart';
import 'package:flutter_demo/ui/pages/read/widgets/reading_page_widget.dart';
import 'package:get/get.dart';

import 'index.dart';

//小说翻页
class ReadPage extends GetView<ReadController> {
  static const String routeName = "/ReadPage";
  const ReadPage({Key? key}) : super(key: key);

  // 主视图
  Widget _buildReadingView(BuildContext context) {
    return controller.state.isOver
        ? BookFx(
            size: Size(controller.state.maxWith, controller.state.maxHeight),
            pageCount: controller.state.allPages.length,
            currentBgColor: Colors.white,
            lastCallBack: (index) {
              //controller.loadData();
            },
            previousCallBack: (index) {
              if (index == 0) {
                return;
              }
            },
            nextPage: (index) {
              /// 下一页
              return index < controller.state.allPages.length - 1
                  ? ReadingPageWidget(index: index, color: Colors.white)
                  : const SizedBox();
            },
            currentPage: (int index) {
              /// 当前页 index 页码
              return ReadingPageWidget(index: index, color: Colors.white);
            },
            controller: controller.state.bookController,
          )
        : const Center(
            child: Text(
              "加载中...",
              style: TextStyle(color: Colors.black, fontSize: 20),
            ),
          );
  }

  // 主视图
  Widget _buildView(BuildContext context) {
    TextEditingController textEditingController = TextEditingController();
    return Column(
      children: [
        /// 电子书
        _buildReadingView(context),

        Row(
          mainAxisAlignment: MainAxisAlignment.spaceAround,
          children: [
            ElevatedButton(
                onPressed: () {
                  controller.state.bookController.next();
                },
                child: Text("下一页")),
            ElevatedButton(
                onPressed: () {
                  controller.state.bookController.last();
                },
                child: Text("上一页")),
            ElevatedButton(
                onPressed: () {
                  Get.defaultDialog(
                      title: "标题",
                      titleStyle: const TextStyle(color: Colors.red),
                      content: Column(
                        children: [
                          TextField(
                            controller: textEditingController,
                            textInputAction: TextInputAction.go,
                            keyboardType: TextInputType.number,
                          )
                        ],
                      ),
                      textCancel: "取消",
                      textConfirm: "确认",
                      confirmTextColor: Colors.white,
                      onConfirm: () {
                        int index = int.parse(textEditingController.text);
                        controller.state.bookController.goTo(index);
                        //这是关闭对话框
                        Navigator.pop(context);
                      });
                },
                child: Text("跳转指定页")),
          ],
        )
      ],
    );
  }

  @override
  Widget build(BuildContext context) {
    return GetBuilder<ReadController>(
      builder: (_) {
        return Scaffold(
          resizeToAvoidBottomInset: false,
          appBar: AppBar(title: const Text("read")),
          body: SafeArea(
            child: _buildView(context),
          ),
        );
      },
    );
  }
}
